package com.hinotoyk.testsystem.service;

import com.hinotoyk.testsystem.entity.StuAnswersInfo;
import com.hinotoyk.testsystem.entity.StuAnswersInfoExample;
import com.hinotoyk.testsystem.mappers.StuAnswersInfoMapper;
import com.hinotoyk.testsystem.mappers.StudentMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StudentAnswerService {
    @Resource
    private StuAnswersInfoMapper mapper;

    public StuAnswersInfo FindStuAnswersInfoById(int tid, int sid){
        StuAnswersInfoExample e = new StuAnswersInfoExample();
        StuAnswersInfoExample.Criteria criteria = e.createCriteria();
        criteria.andTestPaperIdEqualTo(tid);
        criteria.andStudentIdEqualTo(sid);
        List<StuAnswersInfo> list = mapper.selectByExample(e);
        if(list != null && list.size() > 0)
            return list.get(0);
        return null;
    }

    public boolean UpdateStuAnswersInfo(StuAnswersInfo info){
        StuAnswersInfoExample e = new StuAnswersInfoExample();
        StuAnswersInfoExample.Criteria criteria = e.createCriteria();
        criteria.andTestPaperIdEqualTo(info.getTestPaperId());
        criteria.andStudentIdEqualTo(info.getStudentId());
        return mapper.updateByExample(info, e) != 0;
    }

    public boolean insertStuAnwersInfo(int studentId,int tpiId){
        StuAnswersInfo sai = new StuAnswersInfo();
        sai.setStudentId(studentId);
        sai.setTestPaperId(tpiId);
        sai.setStatus(0);
        sai.setScore(0);
        int insert = mapper.insert(sai);
        return insert !=0;
    }
}
